import Vue from 'vue'
import Vuex from 'vuex'
import {getAdminInfo, queryBalanceType, queryPeriod, queryEmpsOfSchoolid,queryBalanceItem,queryGift} from '@/api/getData'
import {querySchool} from "../api/getData";

Vue.use(Vuex)

const state = {
    userinfo:{},
    options: [],
    subjectList:[{id:1,name:"BREAKIN"},{id:2,name:"JAZZ"},{id:3,name:"POPPIN"},{id:4,name:"HIPHOP"}],
    giftList:null,
    paytypeList:[{id:1,name:"无非现金"},{id:2,name:"刷卡"},{id:3,name:"支付宝"},{id:4,name:"微信"},{id:5,name:"其他"}
    ,{id:6,name:"转账"},{id:7,name:"预存转账"}],
    wayidList:[{id:1,name:"现金"},{id:2,name:"刷卡"},{id:3,name:"支付宝"},{id:4,name:"微信"},{id:6,name:"转账"},{id:5,name:"其他"},{id:7,name:"预存转项目"}],

    usertypeList:[{id:1,name:"会员客户"},{id:2,name:"普通客户"}],
    productList:[{id:1,name:"产品A"},{id:2,name:"产品B"}],
    onusertypeList:[{id:3,name:'正式生'},{id:4,name:'短期生'}],
    years:[{id:2018},{id:2017},{id:2016},{id:2015},{id:2014}],
    months:[{id:1},{id:2},{id:3},{id:4},{id:5},{id:6},{id:7},{id:8},{id:9},{id:10},{id:11},{id:12}],
    weeks:[{id:1},{id:2},{id:3},{id:4},{id:5}],
    days:[{id:1,name:"周一"},{id:2,name:"周二"},{id:3,name:"周三"},{id:4,name:"周四"},{id:5,name:"周五"},{id:6,name:"周六"},{id:7,name:"周日"}],
    optionsMenu: {"giftmanage":[{"id":120,"rulepath":"/giftList",rulename:"赠品管理"}],"updatePwd":[{"id":100,"rulepath":"/updatePwd",rulename:"密码管理"}],"scoremanage":[{"id":78,"rulepath":"/thescorelist",rulename:"积分管理"},{"id":79,"rulepath":"/customerConsume",rulename:"客户购买"}],"usermanage":[{"id":1,"rulepath":"/employeeList","rulename":"会员列表","menu":"usermanage"},{"id":3,"rulepath":"/addEmployee","rulename":"新增会员","menu":"usermanage"}]},
    set_active_index: '',
    schoolList: null,
    ruleList:null,
    masterschoolList:[{id:99999,name:"XRAY-街舞连锁"}],
    querytimelist:[{id:1,name:"试课/建档时间"},{id:2,name:"缴费/退费时间"},{id:3,name:"正式上课时间"},{id:4,name:"学期结束时间"},{id:5,name:"编辑档案时间"},{id:6,name:"近一周过生日的"}],
    chargestatuslist:[{id:1,name:"交费"},{id:2,name:"退费"}],

    //雇员列表
    employeeList: [],
    sexList: [{type: 'm', name: "男"}, {type: 'f', name: "女"}],

    dutyList: [{id: '1', name: "总校长"}, {id: '2', name: "分校长"}, {id: '3', name: "教务老师"}, {id: '4', name: "教学老师"}],
    workStatusList: [{id: 1, name: "在职"}, {id: 2, name: "离职"}],
    //收支类型
    chargeTypeList: [{id: 1, name: "收入"}, {id: 2, name: "支出"}],
    //收支方式
    chargeWay: [],
    cardList:[{id:1,name:"卡A",charge:"2999"},{id:2,name:"卡B",charge:'5000'}],
    //收支类别
    balanceTypeList: null,
    //收支项目状态
    balanceItemStatus: [{id: 1, name: "项目进行中"}, {id: 2, name: "项目结束"}],
    channelList: [{id: 1, name: "来校报名"}, {id: 2, name: "学校活动"}, {id: 3, name: "外出宣传"}, {id: 4, name: "内转介绍"}, {
        id: 5,
        name: "业内机构"
    }, {id: 6, name: "网上报名"}, {id: 7, name: "其他方式"}],
    feeTypeList: [],
    balanceItemList: null,
    periodList: null,
    empschoolMap: null,
    userStatusMap:{0:'删除',1:'在用',2:'暂停'},
    usertypeMap:{1:'潜在生',2:'试听生',3:'正式生',4:'短期生',5:'退费生',6:'不续费学生',7:'暂停学生'},
    adminInfo:null,
    /*adminInfo: {
        avatar: 'default.jpg'
    },*/
}

const getters = {
    sexList: state => state.sexList,
    dutyList: state => state.dutyList,
    workStatusList: state => state.workStatusList,
    schoolList: state => state.schoolList,
    masterschoolList:state=>state.masterschoolList,
    employeeList: state => state.employeeList,
    chargeTypeList: state => state.chargeTypeList,
    balanceItemStatus: state => state.balanceItemStatus,
    chargeWay: state => state.chargeWay,
    feeTypeList: state => state.feeTypeList,
    balanceTypeList: state => state.balanceTypeList,
    balanceItemList: state => state.balanceItemList,
    ruleList: state => state.ruleList,
    channelList: state => state.channelList,
    usertypeList:state=>state.usertypeList,
    onusertypeList:state=>state.onusertypeList,
    periodList: state => state.periodList,
    subjectList:state=>state.subjectList,
    giftList:state=>state.giftList,
    cardList:state=>state.cardList,
    empschoolMap: state => state.empschoolMap,
    optionsMenu: state => state.optionsMenu,
    userinfo:state=>state.userinfo,
    paytypeList:state=>state.paytypeList,
    yearsList:state=>state.years,
    daysList:state=>state.days,
    monthsList:state=>state.months,
    weeksList:state=>state.weeks,
    querytimelist:state=>state.querytimelist,
    chargestatuslist:state=>state.chargestatuslist,
    wayidList:state=>state.wayidList,
    adminInfo:state=>state.adminInfo,
    userStatusMap:state=>state.userStatusMap,
    usertypeMap:state=>state.usertypeMap,
    productList:state=>state.productList,

}

const mutations = {
    saveAdminInfo(state, adminInfo) {
        state.adminInfo = adminInfo;

    },
    balanceTypes(state, data) {
        state.balanceTypeList = data;
    },
    setBalanceItems(state,data){
      state.balanceItemList=data;
    },

    periodList(state, data) {
        state.periodList = data;
    },
    setGiftList(state,data){
        state.giftList=data;
    },
    setSubjectList(state,data){
        state.subjectList=data;
    },
    deleteEmpschool(state, {schoolid}) {
        state.empschoolMap.remove(schoolid);
    },
    setEmpschool(state, data) {
        state.empschoolMap = data;
    },
    add_tabs(state, data) {
        this.state.options.push(data);
    },
    setschoolList(state,data){
      this.state.schoolList=data;
    },
    setMenuRles(state,data){
      this.state.optionsMenu=data;
    },
    setRuleList(state,data){
      this.state.ruleList=data;
    },
    setusertypeList(state,data){
      this.state.usertypeList=data;
    },
    refreshMenu(state){
        if(this.state.adminInfo!=null){

        }
    },

    add_manytabs(state, data) {
        var opts = [];
        this.state.options=[];
        data.forEach(item => {
            opts.push(item);
            this.state.options.push(item);
        });
        localStorage.setItem("options",JSON.stringify(opts));



    },

    // 删除tabs
    delete_tabs(state, route) {
        let index = 0;
        for (let option of state.options) {
            if (option.route === route) {
                break;
            }
            index++;
        }
        this.state.options.splice(index, 1);
    },
    // 设置当前激活的tab
    set_active_index(state, index) {
        this.state.activeIndex = index;
    },


}

const actions = {


    deleteEmpschool({commit}, {schoolid}) {
        commit(deleteEmpschool, {schoolid});
    },
    async fillSchoolEmps({commit},reset) {

        var v = state.empschoolMap;
        if (v == null||reset==true) {
            try {
               var operuser=this.adminInfo;
               if(operuser==null)
                   operuser=JSON.parse(localStorage.getItem("adminInfo"));
               //alert("2:"+JSON.stringify(operuser));
                const res = await queryEmpsOfSchoolid({"data": {"id": operuser.id}})
                if (res.success == true) {
                    commit('setEmpschool', res.data);
                } else {
                    throw new Error(res)
                }
            } catch (err) {
                console.log(err)
            }
        }
    },

    async getAdminData({commit}) {
        try {
            const res = await getAdminInfo()
            if (res.success == true) {
                commit('saveAdminInfo', res.data);
            } else {
                throw new Error(res)
            }
        } catch (err) {
            console.log('您尚未登陆或者session失效')
        }
    },




    async getBalanceTypes({commit}) {
        const res = await queryBalanceType({"data": {"pageno": 1, "pagesize": 50}});
        if (res.success == true) {
            commit('balanceTypes', res.data.values);
        }
        else {
            alert("获取balanceTypes类型!")
        }
    },
    async getBalanceItem({commit}) {

        const res = await queryBalanceItem({"data": {"pageno": 1, "pagesize": 50}});
        if (res.success == true) {

            commit('setBalanceItems', res.data.values);
        }
        else {
            alert("获取balanceTypes类型!")
        }
    },

    async getPeriod({commit}) {

        const res = await queryPeriod({"data": {"pageno": 1, "pagesize": 100}});
        if (res.success == true) {
            //alert(JSON.stringify(res.data.values));
            commit('periodList', res.data.values);
        }
        else {
            alert("获取balanceTypes类型!")
        }
    },
    async getSubjectList({commit}){
        const res = await querySubject({"data": {"pageno": 1, "pagesize": 100}});
        if (res.success == true) {
            commit('setSubjectList', res.data.values);
        }
        else {
            alert("获取subjectList类型失败!!")
        }
    },
    async getGiftList({commit}){
        const res = await queryGift({"data": {"pageno": 1, "pagesize": 100}});
        if (res.success == true) {
            commit('setGiftList', res.data.values);
        }
        else {
            alert("获取getGiftList类型失败!!")
        }
    },




    async getAllSchool({commit}){
        var operuser=this.adminInfo;
        if(operuser==null)
            operuser=JSON.parse(localStorage.getItem("adminInfo"));
        const res = await querySchool({"data": {"pageno": 1, "pagesize": 1000,"eid":operuser.id,"duty":operuser.duty}});
        if (res.success == true) {
            commit('setschoolList', res.data.values);
        }
        else {
            alert("获取学校列表失败!")
        }
    },
    async getAllUserType({commit}){
        const res=await queryPeriod({"data":{"pageno":1,"pagesize":1000}});
        if(res.success==true){
            commit('setusertypeList',res.data.values);
        }

    }

}

export default new Vuex.Store({
    state,
    actions,
    mutations,
    getters,
})
